<?php
use PHPSocketIO\SocketIO;
use Workerman\Worker;

// composer autoload
require_once __DIR__ . '/vendor/autoload.php';

$io = new SocketIO(2020);
$io->on('connection', function ($socket) {
    echo "new connection coming\n";
    $socket->addedUser = false;

    // when the client emits 'new message', this listens and executes
    $socket->on('new message', function ($data) use ($socket) {
        echo $data . "\n";
        // 广播给除啦自己的所有客户端
        $socket->broadcast->emit('new message', array(
            'username' => $socket->username,
            'message'  => $data,
        ));
        //系统回复一个消息
        $socket->emit('typing', array(
            'username' => 'System',
            'message'  => '上条消息发送时间:' . date('Y-m-d H:i:s'),
        ));
    });

    // when the client emits 'add user', this listens and executes
    $socket->on('add user', function ($username) use ($socket) {
        global $usernames, $numUsers;
        // we store the username in the socket session for this client
        $socket->username = $username;
        // add the client's username to the global list
        $usernames[$username] = $username;
        ++$numUsers;
        $socket->addedUser = true;
        $socket->emit('login', array(
            'numUsers' => $numUsers,
        ));
        // echo globally (all clients) that a person has connected
        $socket->broadcast->emit('user joined', array(
            'username' => $socket->username,
            'numUsers' => $numUsers,
        ));
    });

    // when the client emits 'typing', we broadcast it to others
    $socket->on('typing', function () use ($socket) {
        $socket->broadcast->emit('typing', array(
            'username' => $socket->username,
        ));
    });

    // when the client emits 'stop typing', we broadcast it to others
    $socket->on('stop typing', function () use ($socket) {
        $socket->broadcast->emit('stop typing', array(
            'username' => $socket->username,
        ));
    });

    // when the user disconnects.. perform this
    $socket->on('disconnect', function () use ($socket) {
        global $usernames, $numUsers;
        // remove the username from global usernames list
        if ($socket->addedUser) {
            unset($usernames[$socket->username]);
            --$numUsers;

            // echo globally that this client has left
            $socket->broadcast->emit('user left', array(
                'username' => $socket->username,
                'numUsers' => $numUsers,
            ));
        }
    });

});

if (!defined('GLOBAL_START')) {
    Worker::runAll();
}
